var UserPlantation = function(rawObject,shouldCollide){


    Drawable.call(this,rawObject,shouldCollide);
    this.width = 50;
    this.height = 50;
    this.plantation = rawObject.plantation;
    this.maturity = rawObject.maturity;
    this.tile = rawObject.tile;
    this.health = rawObject.health;
    this.real_health = rawObject.real_health;
    this.real_maturity = rawObject.real_maturity;
    this.x = this.tile.x;
    this.y = this.tile.y;
    this.z = 0;

    this.src = "/images/plantation/" + this.plantation.name + "/" + this.maturity.name+ "-" + this.health.name +".png";
    this.spriteSheet;
    this.name = this.plantation.name;


    // timer
    this.consumeTime = 5;// secondes
    this.humidityConsumation = 1;
    this.fertilityConsumation = 1;
    this.decayPenality = this.plantation.decayTime/100; // the crop decay

    this.decaying = false;
    this.growing = true;
    this.startedHolding = false; // the crop is fully mature or can't grow anymore due to lack of nutriements and it's not yet decaying

    this.decayTimer = null;
    this.growTimer = null;
    this.consumeTimer = null;
    this.startDecayTimer = null;
    this.sheetInfo = this.getCoorFor(this.maturity.name);
    this.img = new Image();
}

UserPlantation.prototype = {

    getCoorFor : function(name){
        if(name == "seeded")
            return {sx: 0,sy: 0,sw : 50, sh: 43, dx :0, dy: 0,dw: 50, dh: 43}
        if(name == "seedlings")
            return {sx: 0,sy: 0,sw : 50, sh: 43, dx :0, dy: 0,dw: 50, dh: 43}
        if(name == "little_plants")
            return {sx: 0,sy: 0,sw : 50, sh: 50, dx :0, dy: 0,dw: 50, dh: 50}
        if(name == "plant")
            return {sx: 0,sy: 0,sw : 50, sh: 53, dx :0, dy: 0,dw: 50, dh: 50}
        if(name == "mature")
            return {sx: 0,sy: 0,sw : 50, sh: 50, dx :0, dy: 0,dw: 50, dh: 50}
    },

    load : function(engine, densityMap){

        this.loadImg(engine);

        this.consumeTimer = setInterval(this.consume.bind(this),this.consumeTime*1000);
        this.growTimer = setInterval(this.grow.bind(this),1000);
        this.growing = true;
    },

    loadImg : function(engine){
        this.sheetInfo = this.getCoorFor(this.maturity.name);
        this.src = "/images/plantation/" + this.plantation.name + "/" + this.maturity.name+"-"+ this.health.name +".png";
        var self = this;

        if (this.spriteSheet != null)
            this.spriteSheet.destroy();

        console.log(this.tile);
        this.spriteSheet = new engine.Sheet({x:this.tile.x,y:this.tile.y,z:0}, {alphaD:0,betaD:0,gammaD:45},
            {w:50,h:50});

        this.img.onload = function(){
            self.spriteSheet.context.drawImage(self.img,self.sheetInfo.sx,self.sheetInfo.sy,self.sheetInfo.sw,
                self.sheetInfo.sh,self.sheetInfo.dx,self.sheetInfo.dy,self.sheetInfo.dw,self.sheetInfo.dh);


            self.spriteSheet.canvasChanged();

            engine.calc.calculateChangedSheets();
            engine.drawing.drawScene(true);


        }
        this.img.src = this.src;
    },

    consume : function(){

        if (!this.tile.consume(this.humidityConsumation,this.fertilityConsumation)){

            if (!this.decaying && !this.startedHolding){ // first fail
                this.startDecayTimer = setInterval(this.startDecay.bind(this),this.plantation.decayTime);
                clearInterval(this.growTimer); // it' don't grow anymore
                this.growing = false;
            }

        }else{
            if (!this.growing){
                this.growTimer = setInterval(this.grow.bind(this),1000);
                if (this.startedHolding)
                    clearInterval(this.startDecayTimer);
                else
                    clearInterval(this.decayTimer);

                this.decaying = false;
                this.startedHolding = false;
                this.growing = true;
            }
        }

    },

    startDecay : function(){
        clearInterval(this.startDecayTimer);
        this.startedHolding = false;
        this.decayTimer = setInterval(this.decay.bind(this),1000);
        this.decaying = true;
    },


    decay : function(){
        this.real_health -= this.decayPenality;
        if (this.real_health <= 0){
            this.kill();
            return;
        }

        if (this.real_health < this.health.min){
            this.health = this.getHealthDown(this.health.name);
            if (this.health != undefined){
                this.loadImg(World.drawer.engine);
                this.save();
            }
        }
    },

    kill : function(){

        clearInterval(this.consumeTimer);
        clearInterval(this.decayTimer);
        clearInterval(this.growTimer);
        this.delete();

    },

    grow : function(){

        this.growing = true;
        this.real_maturity += this.plantation.growRate / 300;
        if (this.real_maturity >= 100){
            clearInterval(this.growTimer);
            clearInterval(this.consumeTimer);
            this.startDecayTimer = setInterval(this.startDecay.bind(this),this.plantation.decayTime*1000);
            this.startedHolding = true;
            return;
        }
        if (this.real_maturity > this.maturity.max){
            this.maturity = this.getMaturityUp(this.maturity.name);
            this.loadImg(World.drawer.engine);
            this.save();
        }

    },

    getMaturityUp : function(name){

        if (name == "seeded"){
            return World.getFor(World.maturity,"name","seedlings");
        }
        if (name == "seedlings"){
            return World.getFor(World.maturity,"name","little_plants");
        }
        if (name == "little_plants"){
            return World.getFor(World.maturity,"name","plant");
        }
        if (name == "plant"){
            return World.getFor(World.maturity,"name","mature");
        }


    },
    save : function(){
        var save = {};
        save.id = this.id;
        save.tile = this.tile.id;
        save.plantation = this.plantation.id;
        save.maturity = this.maturity.id;
        save.health = this.health.id;
        save.real_health = this.real_health;
        save.real_maturity = this.real_maturity;
        World.instance.networkManager.plant(save);
    },

    getHealthUp : function(name){

        if (name == "bad"){
            return World.getFor(World.healths,"name","low");
        }
        if (name == "low"){
            return World.getFor(World.healths,"name","average");
        }
        if (name == "average"){
            return World.getFor(World.healths,"name","medium");
        }
        if (name == "medium"){
            return World.getFor(World.healths,"name","good");
        }


    },

    getHealthDown : function(name){

        if (name == "good"){
            return World.getFor(World.healths,"name","medium");
        }
        if (name == "low"){
            return World.getFor(World.healths,"name","bad");
        }
        if (name == "average"){
            return World.getFor(World.healths,"name","low");
        }
        if (name == "medium"){
            return World.getFor(World.healths,"name","average");
        }


    },

    getInfos : function(){
        var infos = new Array();

        infos["Humidité"] = this.tile.humidity + "%";
        infos["Fertilité"] = this.tile.fertility + "%";
        infos["Fertilité max"] = this.tile.maxFertility + "%";
        infos["Santé"] = this.health.name+" ("+ this.real_health + "%)";
        infos["Maturité"] = this.maturity.name+" ("+ this.real_maturity + "%)";



        return infos;
    },

    isSelected : function(engine,puv){

        var ouv = engine.drawing.getPointuv(this.spriteSheet.centerp);



        if (puv.u > ouv.u - 18  &&
            puv.u < ouv.u - 18 + 36  &&
            puv.v > ouv.v - 18  &&
            puv.v < ouv.v - 18 + 36){
            return true;
        }

        return false;
    }

    ,
    getSheet : function(){
        return  this.spriteSheetObject = null;
    },

    getImage : function(){
        return this.img;
    },


    drawInInfo :function(context){
        var self = this;
        context.drawImage(self.spriteSheet,self.spriteIndexX,self.spriteIndexY,48,92,0,0,48,92);
    },
    getCuttingInfo : function(){
        return {
            from : 0,
            to : 0,
            width: 48,
            height : 92
        }
    },

    delete : function(){
            this.spriteSheet.destroy();
            this.showing = false;
        World.userPlants.splice(World.userPlants.indexOf(this),1);
        World.drawer.engine.calc.calculateChangedSheets();
        World.drawer.engine.drawing.drawScene(true);
        World.instance.networkManager.deletePlant(this.id);
        delete this;
    }





}